//
//  HMHomeTableViewCell.swift
//  swift微博项目
//
//  Created by huodian on 16/9/27.
//  Copyright © 2016年 huodian. All rights reserved.
//

import UIKit

import SDWebImage

let pictureCellWidthItem : CGFloat = 3

let pictureMargin:CGFloat = 8


let pictureViewWidthItem = UIScreen.main.bounds.width - (2 * pictureMargin)

let pictureWidth = (pictureViewWidthItem - (2 * pictureCellWidthItem))/3







class HMHomeTableViewCell: UITableViewCell {
  
    @IBOutlet weak var retweedLable: UILabel!
    
    @IBOutlet weak var attitudeLable: UIButton!
    
    @IBOutlet weak var reportLable: UIButton!
    @IBOutlet weak var commentLable: UIButton!
    
    @IBOutlet weak var pictureViewTopCon: NSLayoutConstraint!
    @IBOutlet weak var pictureView: HMPictureView!
    
    @IBOutlet weak var picktureViewHeight: NSLayoutConstraint!
    @IBOutlet weak var picktureViewWidth: NSLayoutConstraint!

    @IBOutlet weak var contentLable: UILabel!
    @IBOutlet weak var souceLable: UILabel!
    @IBOutlet weak var timeLable: UILabel!
    @IBOutlet weak var avatarImage: UIImageView!
    @IBOutlet weak var memberImage: UIImageView!
    @IBOutlet weak var nameLable: UILabel!
    @IBOutlet weak var userImage: UIImageView!
    
    
    @IBOutlet weak var pictureLayout: UICollectionViewFlowLayout!
    
    
    @IBOutlet weak var bottomVIew: UIView!
    
    func rowHeight(viewModel:HMStatuesCellModel)->CGFloat{
        
        
        self.viewModel = viewModel
        
        self.contentView.layoutIfNeeded()
        
        
        return bottomVIew.frame.maxY
        
        
    }
    
    var viewModel:HMStatuesCellModel? {
        
        didSet{
            
            nameLable.text = viewModel?.statues?.user?.name
            
            souceLable.text = viewModel?.dealTheSource()
            
           
            //设置时间
            timeLable.text = viewModel?.creatTime
            
            contentLable.text = viewModel?.statues?.text
            
            userImage.sd_setImage(with: viewModel?.iconName)
            
            avatarImage.image = viewModel?.vartarImage
            
            memberImage.image = viewModel?.memberImage
            
            let lev  = viewModel?.pictureInfo?.count ?? 0
            
            
            let sizes = setThepictureVIewSize(count: lev)

            
            self.picktureViewWidth.constant = sizes.pSize.width
            
            self.picktureViewHeight.constant = sizes.pSize.height
            
             pictureLayout.itemSize = sizes.itemSize
            
          
            pictureView.pictureInfos = viewModel?.pictureInfo
            
            if lev == 0 {
                
                pictureViewTopCon.constant = 0
                
            }
            
            attitudeLable.titleLabel?.text = getThebottomViewMessage(count: viewModel?.statues?.attitude_count ?? 0, defaultTitle: "点赞")
            
            commentLable.titleLabel?.text = getThebottomViewMessage(count: viewModel?.statues?.comments_count ?? 0, defaultTitle: "评论")
            
            reportLable.titleLabel?.text = getThebottomViewMessage(count: viewModel?.statues?.reposts_count ?? 0, defaultTitle: "转发")
           
            
            retweedLable?.text = viewModel?.statues?.retweeted_status?.text
            
        }
        
        
        
        
    }
    //解决品论数量问题
    func getThebottomViewMessage(count:Int,defaultTitle:String)->String{
        
        if count == 0 {
            
            return defaultTitle
        }
        
        if count > 10000 {
            
            return "\(Double(count/1000)/10)万"
            
        }
    
        
        return "\(count)"
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.selectionStyle = .none
        
      retweedLable?.preferredMaxLayoutWidth = UIScreen.main.bounds.size.width - 2 * 8
        
        contentLable.preferredMaxLayoutWidth = UIScreen.main.bounds.size.width - 2 * 8
        
       
        
        pictureLayout.minimumLineSpacing = pictureCellWidthItem
        
        pictureLayout.minimumInteritemSpacing = pictureCellWidthItem
        
     
     
    }

    
    //返回可视视图的尺寸
    func setThepictureVIewSize(count:Int) -> (pSize : CGSize,itemSize : CGSize)
    {
        
        if count == 0 {
            
            return (CGSize.zero,CGSize.zero)
            
        }
        
        if count == 1 {
            let imageStr  = viewModel?.pictureInfo?.first?.thumbnail_pic ?? ""
            
            
            
            
            
            let image  =
            SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: imageStr)
            
            var  imageRect = CGSize(width: 50, height: 50)
            
            
            if image?.size != nil {
                
                imageRect = image!.size
                
            }
            
            
            print(image?.size)
            
            print("&&&&&&&&")
            
            
            
            return (imageRect,imageRect)
            
            
            
        }
        if count == 4 {
            
            let width = pictureWidth * 2 + pictureCellWidthItem
            
            
            return (CGSize(width: width, height: width),CGSize(width: pictureWidth, height: pictureWidth))
            
        }
        
        let num = CGFloat((count - 1 )/3 + 1)
        
        
        return (CGSize(width: pictureViewWidthItem, height: pictureWidth * num + pictureCellWidthItem * (num - 1)),CGSize(width: pictureWidth, height: pictureWidth))
        
        
        
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        
    }
    
}
